<template>
  <a-modal
    title="移动部门"
    :width="480"
    :maskClosable="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" v-bind="formItemLayout">
        <a-form-item
          v-show="!record || (record && record[UserPickerOptions.pid] !== UserPickerOptions.root)"
          label="目标部门"
        >
          <a-tree-select
            style="width: 100%"
            :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
            :treeData="treeData"
            treeDefaultExpandAll
            dropdownMatchSelectWidth
            v-decorator="[
              'orgId',
              {
                rules: [{ required: true, message: '请选择目标部门！' }]
              }
            ]"
          >
            <span slot="title" slot-scope="p">
              {{ p.name }}
            </span>
          </a-tree-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { moveUsers } from '@/api/authenticationCenter/structure';
import { setArrayToTree } from '@/utils/util';
import { UserPickerOptions } from '@/components/UserPicker/options';
import cloneDeep from 'lodash.clonedeep';

export default {
  name: 'UserMove',
  data() {
    return {
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        }
      },
      record: null,
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      UserPickerOptions
    };
  },
  props: {
    partyList: {
      type: Array,
      default: []
    },
    orgId: {
      type: Number,
      required: true
    }
  },
  computed: {
    treeData() {
      const partyList = cloneDeep(this.partyList);
      partyList.forEach(party => {
        party.scopedSlots = { title: 'title' };
        party.value = party.id;
      });

      // 组织机构部门树状结构
      const tree = setArrayToTree({
        ...UserPickerOptions,
        data: partyList,
        collection: 'children'
      });

      return tree;
    }
  },
  methods: {
    open(record) {
      this.visible = true;
      this.record = record;
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this;
      this.confirmLoading = true;
      validateFields(async (errors, values) => {
        if (!errors) {
          const req = { ...values };
          req.id = this.record;
          req.orgId = values.orgId;
          req.srcId = this.orgId;

          const res = await moveUsers(req);
          this.confirmLoading = false;
          if (res.code === 'sso.ok') {
            this.$emit('ok');
            this.handleCancel();
            this.$message.success(res.message);
          }
        } else {
          this.confirmLoading = false;
        }
      });
    },
    handleCancel() {
      if (this.confirmLoading) return;
      this.visible = false;
      this.record = null;
      const {
        form: { resetFields }
      } = this;
      resetFields();
    }
  }
};
</script>
